using System;
using System.Collections;
using System.Configuration;
using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Helpers;
using Saffron.Framework.Users;
using DogBite.Dachshund.Configuration;

namespace Saffron.Components
{
    public class NavigationComponent : ViewComponent
    {

        public NavigationList CurrentList
        {
            get 
            {
                UserAccount user = (UserAccount)Session["user"];
                if (user != null && user.IsInRole(SystemRoleType.User.ToString()))
                    return DachshundSectionHandler.GetNavigationList("overallUser");

                return DachshundSectionHandler.GetNavigationList("overallGuest");
            }
        }

        public NavigationCollection SubNavigations
        {
            get
            {
                string currentController = RailsContext.CurrentController.Name;
                foreach (Navigation nav in CurrentList.Navigations)
                {
                    if (nav.Controller == currentController)
                        return nav.Navigations;
                }

                return null;
            }
        }

        public string ActiveControllerName
        {
            get
            {
                return RailsContext.CurrentController.Name;
            }
        }

        public override void Initialize()
        {
            

            base.Initialize();
        }

        public override void Render()
        {
            PropertyBag["activeController"] = ActiveControllerName;
            PropertyBag["navs"] = CurrentList.Navigations;
            PropertyBag["subNavs"] = SubNavigations;
            
            
            base.Render();
        }

        public override bool SupportsSection(string name)
        {
            return base.SupportsSection(name);
        }
    }
}
